//
//  YKSearchNormalDataByClientViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/2.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKSearchNormalDataByClientViewController.h"
#import "YKDataHeaderView.h"
#import "YKMultiSelectDropdownView.h"
#import "YKSaleModel.h"
#import "YKClientListHeaderView.h"
#import "YKSelectDropdownView.h"
#import "YKSelectView.h"
#import "YKSaleItem.h"
@interface YKSearchNormalDataByClientViewController ()
@property (nonatomic, strong) YKSelectView *goodsSelectView;
@property (nonatomic, strong) YKDataHeaderView *headerView;
@property (nonatomic, strong) NSArray *normalSummaryDetail;
@end

@implementation YKSearchNormalDataByClientViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"查询明细";
    // Do any additional setup after loading the view.
    @weakify(self)
    YKDataHeaderView *headerView = [[YKDataHeaderView alloc]initWithType:YKDataHeaderViewTypeYearMonthDay];
    headerView.dateChangeBlock = ^(NSString *start, NSString *end) {
        
        [self_weak_ loadData];
    };
    self.headerView = headerView;
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_top);
    }];
    
    self.goodsSelectView = [[YKSelectView alloc]initWithTitle:@"选择客户" row:[CommonDataManager sharedInstance].guests initialText:[CommonDataManager sharedInstance].guests.firstObject];
    self.goodsSelectView.selectHandler = ^(id<YKDropdown> value) {
        [self_weak_ loadData];
    };
    [self.view addSubview:self.goodsSelectView];
    [self.goodsSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(headerView.mas_bottom);
    }];
    
    YKClientListHeaderView* listHeaderView = [[YKClientListHeaderView alloc]initWithTitles:@[@"品种",@"销量",@"金额"] weights:@[@1,@1,@1]];
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    [self.view addSubview:listHeaderView];
    [listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.goodsSelectView.mas_bottom);
    }];
    listHeaderView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];
    
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.top.equalTo(listHeaderView.mas_bottom);
    }];
    [self loadData];
}


- (void)loadData {
    [self tf_showStateView:kTFViewStateLoading inView:self.tableView];
    [TFHTTPMANAER POST:@"manage/guestSummaryDetail.htm" parameters:@{@"startDate":self.headerView.start, @"endDate": self.headerView.end, @"guestId":_goodsSelectView.selectedRow.code} success:^(id responseObject) {
        
        [self tf_showStateView:kTFViewStateNone inView:self.tableView];
        [self.tableViewManager removeAllSections];
        self.normalSummaryDetail = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"guestSummaryDetail"]];
        
        TFTableViewSection * section = [[TFTableViewSection alloc]init];
        section.headerHeight = .1f;
        section.footerHeight = .1f;
        [section addItemsFromArray:[self.normalSummaryDetail tf_mapUsingBlock:^id _Nullable(id  _Nonnull obj, NSInteger idx) {
            return [YKSaleItem itemWithModel:obj];
        }]];
        if (section.items.count == 0) {
            [self tf_showStateView:kTFViewStateNoData inView:self.tableView];
        }
        [self.tableViewManager addSection:section];
        
        [self.tableView reloadData];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError inView:self.tableView];
    }];
}

@end
